import axios from "axios";
import axiosFile from "axios-file";
import chalk from "chalk";
import { createReadStream } from "fs";

const signApiDev =
  "http://mastest.changan.com.cn/cameap/MucHandleController/getMucSign?empId=1";
const uploadApiDev =
  "http://icadev.changan.com.cn/mam/v5/open/package/uploadH5AndRefAppVersion";

const log = console.log;

const uploadArchive = async ({ env }) => {
  if (env !== "development") {
    log(chalk.red("请手动上传压缩包"));
    return;
  }

  const signRes = await axios.get(signApiDev);
  const signResData = signRes.data;

  if (signResData.code !== 200) {
    log(chalk.red(signResData.msg));
    return;
  }

  const uploadData = Object.assign({ empId: "1" }, signResData.data, {
    file: createReadStream("./dist.zip"),
  });

  const fileRes = await axiosFile({
    url: uploadApiDev,
    method: "post",
    headers: {
      "cache-control": "no-cache",
    },
    data: uploadData,
  });

  const fileResData = fileRes.data;

  if (fileResData.code !== 0) {
    log(chalk.red(fileResData.msg));
  }
};

export default uploadArchive;
